#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re
import os
import random
import time
import datetime
from requests import get
import pymysql 



# user_agent列表
user_agent_list = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
    'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36'
]

# referer列表
referer_list = [
    'http://fund.eastmoney.com/110022.html',
    'http://fund.eastmoney.com/110023.html',
    'http://fund.eastmoney.com/',
    'http://fund.eastmoney.com/110025.html'
]

os.chdir(os.path.abspath(os.path.dirname(__file__)))
with open('jjarr.txt','r',encoding='utf-8') as f:

    starttime = datetime.datetime.now()
    print(starttime)

    data = f.readline()
    data = data.replace("var r = [","")
    jj_com = re.compile("\[(.*?)\]")

    jjArr = re.findall(jj_com,data)

    rl = ''
    for jj in jjArr:
    # jj = re.findall(jj_com,data)[0]
        stop = random.uniform(2, 5)
        time.sleep(stop)
        jj = jj.replace("\"","")
        info = jj.split(",")
        # print(info[3])
        if '债' not in info[3] and '货币' not in info[3]:
            fS_code = info[0]
            print(fS_code)
            # 获取一个随机user_agent和Referer
            headers = {'User-Agent': random.choice(user_agent_list), 'Referer': random.choice(referer_list) }
            html = get("http://fund.eastmoney.com/pingzhongdata/" + fS_code + ".js?v=20160518155842", headers=headers).text
            
            workTime_com = re.compile("workTime\":\"(.*?)年")
            workTimeArr = re.findall(workTime_com,html)
            if workTimeArr and '天' not in workTimeArr[0]:
                # 任职时长
                workTime = int(workTimeArr[0])
                if workTime >= 3:
                    # 基金名称
                    fS_name = re.findall(re.compile("fS_name = \"(.*?)\""),html)[0]
                    # print(fS_name)
                    # 基金编码
                    # fS_code = re.findall(re.compile("fS_code = \"(.*?)\""),html)[0]
                    # print(fS_code)
                    # 业绩评价
                    performanceEvaluationArr = re.findall(re.compile("Data_performanceEvaluation = {\"avr\":\"(.*?)\""),html)
                    if performanceEvaluationArr and '暂无数据' != performanceEvaluationArr[0]:
                        performanceEvaluation = float(performanceEvaluationArr[0])
                        if performanceEvaluation > 70:
                            rl += '基金名称：' + fS_name + '；基金代码：' + fS_code + '\n'
                            print("基金名称：{}；基金代码：{}".format(fS_name,fS_code))
   
    endtime = datetime.datetime.now()
    print(endtime)
    print(rl)
    print('\n数据处理成功!所用时间为：' +  str((endtime - starttime).seconds))
